package com.cy.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * Created by Administrator on 2017/8/12.
 */
public class SendMessageUtil {
    private static Message message;
    private static Address[]address;
    private static Session session;
    private final Logger logger = LoggerFactory.getLogger(Message.class);

    private static class ModulLoginRxbusHolder{
        private static final SendMessageUtil INSTANCE = new SendMessageUtil();
    }

    public SendMessageUtil(){
        try {
            Properties properties = new Properties();
            properties.setProperty("mail.smtp.auth", "true");//实用smtp身份验证
            properties.setProperty("mail.smtp.starttls.enable", "true");
            properties.setProperty("mail.transport.protocol", "smtp");//设置邮件发送协议
            properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            session = Session.getInstance(properties);
            session.setDebug(true);
            message = new MimeMessage(session);
            message.setSubject("已有5分钟未收到kafka的公告推送"); // 主题
            message.setContent("已有5分钟未收到kafka的公告推送", "text/html;charset=gbk"); //前面是文件内容，后面是显示格式
            message.setFrom(new InternetAddress("595254250@qq.com"));//就是哪里发出的
            address=new Address[]{new InternetAddress("294130463@qq.com"),new InternetAddress("644613693@qq.com")};
        } catch (MessagingException e) {
            logger.info("===========================邮件报错了：" + e.getMessage() +"==================================");
        }
    }

    public synchronized void sendMassgae(){
        try {
            Transport t = session.getTransport();
            t.connect("smtp.qq.com", 465, "595254250", "wusvyvmckhvqbbjc");//主机，端口，账号，密码
            t.sendMessage(message, address);//设置发件人和收件人
            t.close();
        } catch (MessagingException e) {
            logger.info("===========================邮件报错了：" + e.getMessage() + "==================================");
        }
    }

    public static SendMessageUtil getInstance(){
        return ModulLoginRxbusHolder.INSTANCE;
    }
}
